package cn.game.tanke.role;

import cn.game.tanke.panel.TankPanel;
import cn.game.tanke.util.TankMap;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Enemy extends Role {
    private static final int MOVE_INTERVAL = 700; // 敌人移动间隔时间，单位：毫秒
    private static final int SHOOT_INTERVAL = 2000; // 敌人射击间隔时间，单位：毫秒
    private Random random;
    private List<Bullet> bullets;
    private TankPanel panel;
    private Thread moveThread;
    private Thread shootThread;
    private boolean isAlive = true;

    public Enemy(TankPanel panel) {
        super(TankMap.enemy, 32, 32, 1, 2, 11, 1);
        this.panel = panel;
        random = new Random();
        bullets = new ArrayList<>();

        // 启动移动线程
        moveThread = new Thread(this::moveContinuously);
        moveThread.start();

        // 启动射击线程
        shootThread = new Thread(this::shootContinuously);
        shootThread.start();
    }

    private void moveContinuously() {
        while (isAlive) {
            try {
                move();
                Thread.sleep(MOVE_INTERVAL);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void shootContinuously() {
        while (isAlive) {
            try {
                shoot();
                Thread.sleep(SHOOT_INTERVAL);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    void move() {
        int x = getX();
        int y = getY();
        int dir = random.nextInt(4); // 随机选择一个方向
        setDir(dir);

        switch (dir) {
            case 0: // 上
                if (isCanMove(x, y - 1)) {
                    setY(y - 1);
                }
                break;
            case 2: // 下
                if (isCanMove(x, y + 1)) {
                    setY(y + 1);
                }
                break;
            case 3: // 左
                if (isCanMove(x - 1, y)) {
                    setX(x - 1);
                }
                break;
            case 1: // 右
                if (isCanMove(x + 1, y)) {
                    setX(x + 1);
                }
                break;
        }
    }

    @Override
    void annimal() {
        // 敌人动画可以在这里实现
    }

    private boolean isCanMove(int x, int y) {
        if (x < 0 || y < 0 || x >= TankMap.MAPARRAY[0].length || y >= TankMap.MAPARRAY.length) {
            return false;
        }
        int blockType = TankMap.MAPARRAY[y][x];
        // 禁止在砖墙、钢墙、冰墙、水域上移动
        if (blockType == 3 || blockType == 4 || blockType == 5) {
            return false;
        }
        return true;
    }

    @Override
    public void drawRole(Graphics graphics, JPanel jPanel) {
        if (isAlive) {
            super.drawRole(graphics, jPanel);
            int x = getX();
            int y = getY();
            if (TankMap.MAPARRAY[y][x] == 2) {
                graphics.drawImage(TankMap.grass, x * 32 - 5, y * 32 - 5, (x + 1) * 32 + 5, (y + 1) * 32 + 5, 0, 0, 87, 83, jPanel);
            }
            // 绘制子弹
            for (int i = 0; i < bullets.size(); i++) {
                Bullet bullet = bullets.get(i);
                if (bullet.isGoOn()) {
                    bullet.drawRole(graphics, jPanel);
                } else {
                    bullets.remove(i);
                    i--;
                }
            }
        }
    }

    public void shoot() {
        if (panel == null ||!isAlive) return;

        int x = getX();
        int y = getY();
        int dir = getDir();

        // 根据方向调整子弹的初始位置
        switch (dir) {
            case 0: // 上
                y = y - 1;
                break;
            case 1: // 右
                x = x + 1;
                break;
            case 2: // 下
                y = y + 1;
                break;
            case 3: // 左
                x = x - 1;
                break;
        }

        // 创建子弹（1表示敌人子弹）
        Bullet bullet = new Bullet(0, x, y, 1, dir, panel);
        panel.getBulletManager().addBullet(bullet);
        bullets.add(bullet);
    }

    public List<Bullet> getBullets() {
        return bullets;
    }

    public void hit() {
        // 处理敌人被击中的逻辑
        System.out.println("敌人被击中！");
        isAlive = false;
        moveThread.interrupt();
        shootThread.interrupt();
        // 可以添加敌人死亡等逻辑
    }

    public boolean isAlive() {
        return isAlive;
    }
}